package com.cssy.yd.logic.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byd.acegiext.domain.User;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.logic.entity.LogicProGetDO;
import com.cssy.yd.logic.entity.LogicProSuggestItemDO;
import com.cssy.yd.logic.service.ILogicProSuggestService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/yd/logic/prosuggest")
public class LogicProSuggestController extends
		BaseController<LogicProGetDO> {
	private static final Logger logger = Logger.getLogger(LogicProSuggestController.class);
			
	private static final String MANAGER = "/yd/logic/prosuggest/Manager";
//	private static final String EIDT = "/yd/logic/prosuggest/Operation";// 添加、修改、显示页面
	private static final String MAIN = "/yd/logic/prosuggest/PsMain";// //首页流程图

	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	
	@Resource(name = "logicProSuggestService")
	private ILogicProSuggestService  logicProSuggestService;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		 if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
			 scs.setOrderBy("dctime desc");
		//scs.addCondition("state","1",SearchCondition.OP_EQUAL);//工单
		SearchResult result = logicProSuggestService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
//		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
//		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
//			model.addAttribute("viewflag", "add");
//			model.addAttribute("xx", 1);// 标示能不能修改内容
//		} else {
//			LogicProGetApplyDO oi = logicProGetApplyService.findListById(id);
//			SearchConditions scs = new SearchConditions();
//			scs.addCondition("get_pro_id",oi.getId(),SearchCondition.OP_EQUAL);
//			scs.setRemovedProperty(null);
//			@SuppressWarnings("unchecked")
//			List<LogicProGetApplyItemDO> loiilist = logicProGetApplyItemService.findListByExample(scs).getResults();
//			if (oi.getBillStatus() == 0) {
//				model.addAttribute("xx", 1);// 标示能不能修改内容
//				model.addAttribute("viewflag", "tijiao");
//			} else {
//				model.addAttribute("xx", 2);// 标示能不能修改内容
//			}
//			if (oi.getBillStatus() == 2) {
//				model.addAttribute("viewflag", "tongguo");
//			}
//			if (oi.getBillStatus() == 1) {
//				model.addAttribute("viewflag", "shenpi");
//			}
//			model.addAttribute("oi", oi);
//			model.addAttribute("axxJson", loiilist);
//		}
//		return EIDT;
		return null;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicProGetDO curAP, BindingResult result, Model model)
			throws Exception {
		return null;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		return null;
	}
	
	/**
	 * 处理为已标记
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deal")
	@ResponseBody
	public Map<String, Object> deal(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
		String ids = request.getParameter("ids");
		String[] fields = ids.split(",");
		for (String field : fields) {
			LogicProSuggestItemDO bean = logicProSuggestService.findListById(field);
			bean.setBillStatus(2);
			User user = SessionUtil.getUser(request);
			bean.setDuuser(user.getName());
			logicProSuggestService.update(bean, SessionUtil.getUser(request));
		}
		bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			logger.warn(e.getMessage(),e);
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		
		return bodyMap.modelMap;
	}
	
	
	/**
	 * 模块首页
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author huang.xuming [2013-9-3]
	 */
	@RequestMapping("/main")
	public String mainpage(HttpServletRequest request,
			HttpServletResponse response, Model model){
		return MAIN;
	}
	
}
